$(function () {
     // 定义状态变量
    let iscontactname=false,
        isemail=false,
        ismessage=false;
    //鼠标离开事件
    //用户名
    let $contactnameRepeat= $('#contactname');
    $contactnameRepeat.blur(fnCheckcontactname);
    function fnCheckcontactname(){
        let scontactname=$contactnameRepeat.val();
        if(scontactname ===''){
            message.showError('用户名不能为空')
            return
        }else {
            iscontactname=true
        }
    };
    //邮箱
    let $emailRepeat= $('#email');
    $emailRepeat.blur(fnCheckemail);
    function fnCheckemail(){
        let semail=$emailRepeat.val();
        if(semail ===''){
            message.showError('邮箱不能为空');
            return
        };
        if(!(/[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/).test(semail)){
            message.showError('邮箱格式有误')
            return
        }else {
            message.showInfo('邮箱格式正确')
            isemail=true
        }
    };
    //内容
    let $messageRepeat= $('#content');
    $messageRepeat.blur(fnCheckcontent)
    function fnCheckcontent(){
        let smessage=$messageRepeat.val();
        if (smessage===''){
            message.showError('内容不能为空');
            return
        }else {
            ismessage=true
        }
    };
    //按钮
    let $button=$('.submit');
    $button.click(function (e){
        //阻止默认提交
        e.preventDefault();
        //检查用户名
        if(!iscontactname){
            fnCheckcontactname();
            return
        }
        //检查邮箱
        if(!isemail){
            fnCheckemail();
            return;
        }
        //检查内容
        if(!ismessage){
            fnCheckcontent();
            return;
        }
        $.ajax(
            {
                url:'/board/contact/',
                type:'POST',
                data:{
                    'contactname':$contactnameRepeat.val(),
                    'email':$emailRepeat.val(),
                    'message':$messageRepeat.val()
                },
                dataType:'json',
                success: function (res) {
                if(res.errno === '0'){
                    message.showInfo('提取成功');
                    setTimeout(function () {
                        //提交成功后重定向到登录页面
                        window.location.href = '/board/contact/'
                    }, 3000)
                }else {
                    //提交失败
                    message.showError(res.errmsg)
                }
                },
                error: function () {
                    message.showError('服务器超时')
                }
            }
        )

    })
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = cookies[i].trim();
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }
    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
            }
        }
    });
});